/**
 * Created by LuoQuan on 2017/11/7.
 */

Ext.define('Cms.website.controller.Login', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.login',

    initCanvas: function (cmp) {
        var cs = cmp.getEl().dom;
        var vm = cmp.up('window').getViewModel();

        Ext.Loader.loadScript({

            url: '/resources/js/verifyCode.js',

            onLoad: function () {
                var captcha = new Ext.Captcha({
                    canvas: cs,
                    width: cs.width,
                    height: cs.height
                });

                vm.set('verifyCode', captcha.generate());
                cs.addEventListener('click', function () {
                    vm.set('verifyCode', captcha.generate());
                })
            }
        });
    },

    reset: function (btn) {
        btn.ownerCt.ownerCt.reset();
    },

    login: function (btn) {
        var form = btn.ownerCt.ownerCt;

        if (!form.getForm().isValid()) {


            Ext.Msg.alert({
                title: '警告',
                msg: '请检查您的输入，确认无误后再提交',
                icon: Ext.Msg.WARNING,
                buttons: Ext.Msg.OK
            });
            return false;
        }

        var inputcode = this.lookupReference('inputcode').getValue();
        var loginwin = this.lookupReference('loginwin');
        var generatedcode = btn.up('window').getViewModel().get('verifyCode');

        if (inputcode === generatedcode) {
            alert('验证码输入正确');
            console.dir(loginwin);
        } else {
            alert('验证码输入不正确');
        }
    }

});
